package com.petShop.controllers;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.petShop.dao.UserDao;
import com.petShop.models.Userinfo;
import com.petShop.util.MyTool;

@Controller
@Transactional
@RequestMapping("/userinfo")
public class UserInfoController {
	@Autowired
	UserDao userDao;
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView get(HttpSession session,HttpServletRequest req){
		ModelAndView mav = new ModelAndView("/userinfo/userinfo");
		String username = (String) session.getAttribute("username");
		Userinfo userinfo = userDao.selectUserByName(username);
		mav.addObject("userinfo",userinfo);
		return mav;
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public ModelAndView post(HttpSession session,@RequestParam String username,
			@RequestParam String phoneNo,@RequestParam String email){
		ModelAndView mav = new ModelAndView("/userinfo/userinfo");
		String name = (String) session.getAttribute("username");
		Userinfo userinfo = userDao.selectUserByName(name);
		
		userinfo.setUsername(username);
		userinfo.setPhoneNo(phoneNo);
		userinfo.setEmail(email);
		
		userDao.updateUser(userinfo);
		Userinfo newUserinfo = userDao.selectUserById(userinfo.getUserId());
		session.setAttribute("username", username);
		mav.addObject("userinfo",newUserinfo);
		mav.addObject("infoMsg","修改成功");
		return mav;
	}
	
	@RequestMapping(value="upload",method=RequestMethod.GET)
	public ModelAndView getUpload(HttpSession session,HttpServletRequest req){
		ModelAndView mav = new ModelAndView("/userinfo/upload");
		String username = (String) session.getAttribute("username");
		Userinfo userinfo = userDao.selectUserByName(username);
		mav.addObject("userinfo",userinfo);
		//获取目前地址
		String returnUrl = req.getRequestURI();
		session.setAttribute("returnUrl", returnUrl);
		return mav;
	}
	
	@RequestMapping(value="upload" ,method=RequestMethod.POST)
	public ModelAndView upload(@RequestParam MultipartFile file,HttpSession session,
			HttpServletRequest request) throws IOException{
		
		ModelAndView mav = new ModelAndView();
		
		String username = (String) session.getAttribute("username");
	    Userinfo userinfo = userDao.selectUserByName(username);

		if(!file.isEmpty()){
		    String path=null;// 文件路径
		    String type=null;// 文件类型
		    String fileName=file.getOriginalFilename();// 文件原名称
		    //System.out.println("上传的文件原名称:"+fileName);
		    // 判断文件类型
		    type=fileName.indexOf(".")!=-1?fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()):null;
		    
		    
		    
		    if (type!=null) {// 判断文件类型是否为空
			    if ("GIF".equals(type.toUpperCase())||"PNG".equals(type.toUpperCase())||"JPG".equals(type.toUpperCase())) {
				    // 项目在容器中实际发布运行的根路径
				    String realPath=request.getSession().getServletContext().getRealPath("/");
				    // 自定义的文件名称
				    String trueFileName=String.valueOf(System.currentTimeMillis())+fileName;
				    // 设置存放图片文件的路径
				    path=realPath+"resources\\images\\userImg\\"+trueFileName;
				    
				    // 转存文件到指定的路径
				    file.transferTo(new File(path));
				    
				    if(!userinfo.getUserImg().equals("headimg.gif")){    //若修改原始头像，则不删除
				    	String oldPath = realPath+"resources\\images\\userImg\\"+userinfo.getUserImg();
				    	MyTool.deleteFile(oldPath);
				    }
				    
				    userinfo.setUserImg(trueFileName);
				    userDao.updateUserImg(userinfo);
				    
				    Userinfo newUserinfo = userDao.selectUserById(userinfo.getUserId());
				    
				    mav.addObject("userinfo",newUserinfo);
				    mav.addObject("fileMsg","修改成功");
			    }else {
				    mav.addObject("fileMsg","文件类型错误,请按要求重新上传");			
				    mav.addObject("userinfo",userinfo);
			    }
		    }else {
		    	mav.addObject("fileMsg","文件类型为空");  
		    	mav.addObject("userinfo",userinfo);
		    }
		}else {
			mav.addObject("fileMsg","没有找到相对应的文件");		
			mav.addObject("userinfo",userinfo);
		}
		mav.setViewName("/userinfo/upload");
		return mav;
	}
	
	

}
